package cn.hzking.gjmall.utils.helpers;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.ToxicBakery.viewpager.transforms.AccordionTransformer;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import cn.hzking.gjmall.R;
import cn.hzking.gjmall.app.Gjmall;
import cn.hzking.gjmall.bean.HomeDataBean;
import cn.hzking.gjmall.utils.UriMatch;
import cn.hzking.gjmall.view.convenientbanner.ConvenientBanner;
import cn.hzking.gjmall.view.convenientbanner.holder.CBViewHolderCreator;
import cn.hzking.gjmall.view.convenientbanner.holder.Holder;
import cn.hzking.gjmall.view.convenientbanner.listener.OnItemClickListener;

/**
 * Created by Administrator on 2018/2/2.
 */

public final class ConvenientBannerHelper {
    private ConvenientBanner convenientBanner;
    private Context context;
    private List<HomeDataBean.DataBean.CarouseBean> carouseBeans;
    private ArrayList<String> localImages = new ArrayList<>();

    public ConvenientBannerHelper(Context context,ConvenientBanner convenientBanner,List<HomeDataBean.DataBean.CarouseBean> arrayList) {
        this.convenientBanner = convenientBanner;
        this.context = context;
        carouseBeans = arrayList;
    }

    public void initConvenientBanner(){
        for (int i = 0; i < carouseBeans.size(); i++) {
            localImages.add(carouseBeans.get(i).getPicUrl());
        }
        //自定义你的Holder，实现更多复杂的界面，不一定是图片翻页，其他任何控件翻页亦可。
        convenientBanner.setPages(
                new CBViewHolderCreator<LocalImageHolderView>() {
                    @Override
                    public LocalImageHolderView createHolder() {
                        return new LocalImageHolderView();
                    }
                }, localImages)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                .setPageIndicator(new int[]{ R.drawable.shape_ic_page_indicator_focused,R.drawable.shape_ic_page_indicator})
                //设置指示器的方向
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setPageTransformer(new AccordionTransformer())//集成特效之后会有白屏现象，新版已经分离，如果要集成特效的例子可以看Demo的点击响应。
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        //todo 轮播图点击事件处理
                        new UriMatch((Activity) context,carouseBeans.get(position).getTargetUrl()).match();
                      //  UIUtils.showToast(context,"第"+position+"个", Toast.LENGTH_SHORT);

                    }
                });
               // .convenientBanner.setManualPageable(false);//设置不能手动影响
        //设置翻页的效果，不需要翻页效果可用不设



    }

    public class LocalImageHolderView implements Holder<String> {
        private ImageView imageView;
        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, String data) {
            Glide.with(context).load(Gjmall.getOssHost()+data).placeholder(R.mipmap.banner).error(R.mipmap.banner).into(imageView);
        }
    }
    //在onResume时调用
    public void startShow(){
         convenientBanner.startTurning(3000);//自动轮播时间
    }

    //在pause时调用
    public void stoptow(){
        convenientBanner.stopTurning();//自动轮播时间
    }
}
